<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author mozar
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class admin extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->load->library("pagination");
        $this->load->model('penduduk_model');
        $this->load->model('user_model');
    }

    function index() {

        $session = $this->session->userdata('login');
        if ($session == null) {
            redirect(site_url());
            exit();
        }
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_admin');
        $this->load->view('page/admin', array('session' => $session));
        $this->load->view('theme/footer');
    }

    function management() {

        $session = $this->session->userdata('login');
        if ($session == null) {
            redirect(site_url());
            exit();
        }
        if ($session['flag'] == 1) {
            
        } else {
            redirect(site_url() . '/admin');
            exit();
        }
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_admin');
        $list_user = $this->user_model->get_list_user();
        $this->load->view('page/admin_management', array('session' => $session, 'list_user' => $list_user));
        $this->load->view('theme/footer');
    }

    function buat_user() {
        $session = $this->session->userdata('login');
        if ($session == null) {
            redirect(site_url());
            exit();
        }
        if ($session['flag'] == 1) {
            
        } else {
            redirect(site_url() . '/admin');
            exit();
        }
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_admin');
        $this->load->view('page/admin_buat_user', array('session' => $session));
        $this->load->view('theme/footer');
    }

    function create_user() {
        $session = $this->session->userdata('login');
        if ($session == null) {
            echo "harus login";
            redirect(site_url());
            exit();
        }
        if ($session['flag'] == 1) {
            
        } else {
            echo "tidak berhak";
            redirect(site_url() . '/admin');
            exit();
        }
        $username = $this->input->post('pengguna_username');
        $pass = $this->input->post('pengguna_password');
        $nama = $this->input->post('pengguna_nama');
        $nip = $this->input->post('pengguna_nip');
        $skpd = $this->input->post('pengguna_skpd');
        $this->load->model('user_model');
        $r = array('result' => 9);
        $r['buat'] = $this->user_model->tambah_pengguna($username, $pass, $nama, $nip, $skpd);
        if ($r['buat'] == true) {
            echo "berhasil";
            redirect(site_url() . '/admin/management');
        } else {
            echo "gagal";
            redirect(site_url() . '/admin/management?error=1');
        }
    }

    function edit_user() {
        $session = $this->session->userdata('login');
        if ($session == null) {
            echo "harus login";
            redirect(site_url());
            exit();
        }
        if ($session['flag'] == 1) {
            
        } else {
            echo "tidak berhak";
            redirect(site_url() . '/admin');
            exit();
        }
        $id_user = (int) $this->input->get('id');
        $user = $this->user_model->get_user_by_id($id_user);
        if ($user == null) {
            echo "user tidak ada";
            redirect(site_url() . '/admin/management');
            exit();
        } else {
            //var_dump($user);
            if ($session['flag'] == 2) {
                echo "tidak berhak mengakses";
                //redirect(site_url() . '/admin/management');
                exit();
            }
        }
        $this->load->view('theme/header');
        $this->load->view('theme/sidebar_admin');
        $this->load->view('page/admin_edit_user', array('session' => $session,'user'=>$user));
        $this->load->view('theme/footer');
    }
    
    function edit_user_simpan(){
        $session = $this->session->userdata('login');
        if ($session == null) {
            echo "harus login";
            redirect(site_url());
            exit();
        }
        if ($session['flag'] == 1) {
            
        } else {
            echo "tidak berhak";
            redirect(site_url() . '/admin');
            exit();
        }
        $id_user = (int) $this->input->post('pengguna_id');
        $user = $this->user_model->get_user_by_id($id_user);
        if($user->flag==2){
            
        }else{
            echo "bukan fungsi yang selayaknya";
            redirect(site_url() . '/admin/management');
            exit();
        }
        $username = $this->input->post('pengguna_username');
        $pass = $this->input->post('pengguna_password');
        $nama = $this->input->post('pengguna_nama');
        $nip = $this->input->post('pengguna_nip');
        $skpd = $this->input->post('pengguna_skpd');
        $user = $this->user_model->update3($id_user,$username,$pass,$nama,$nip,$skpd);
        echo "sukses";
        redirect(site_url() . '/admin/management');
    }
    
    function hapus_user(){
        $session = $this->session->userdata('login');
        if ($session == null) {
            echo "harus login";
            redirect(site_url());
            exit();
        }
        if ($session['flag'] == 1) {
            
        } else {
            echo "tidak berhak";
            redirect(site_url() . '/admin');
            exit();
        }
        $id_user = (int) $this->input->get('id');
        $user = $this->user_model->get_user_by_id($id_user);
        if($user->flag==2){
            $r=$this->user_model->hapus_user($user->pengguna_id);
            redirect(site_url() . '/admin/management');
        }else{
            echo "tidak dapat menghapus super admin";
            redirect(site_url() . '/admin/management');
        }
    }

    function update() {
        $session = $this->session->userdata('login');
        if ($session == null) {
            redirect(site_url());
            exit();
        }
        $username = $this->input->post('pengguna_username');
        $pass_lama = $this->input->post('pengguna_password_lama');
        $pass_baru = $this->input->post('pengguna_password_baru');
        $pass_baru2 = $this->input->post('pengguna_password_baru2');
        $nama = $this->input->post('pengguna_nama');
        $nip = $this->input->post('pengguna_nip');
        $skpd = $this->input->post('pengguna_skpd');
        $this->load->model('user_model');
        $r = array('result' => 9);
        if (strlen($pass_baru) > 0 && strlen($pass_baru2) > 0 ) {
            //mengubah password juga
            if ($pass_baru == $pass_baru2) {
                $r = $this->user_model->update2($username, $pass_lama, $pass_baru, $nama, $nip, $skpd);
                //var_dump($r);
            } else {
                echo "password baru tidak sama";;
                redirect(site_url() . '/admin?error=1');
                exit();
            }
        } else {
            $r = $this->user_model->update($username, $nama, $nip, $skpd);
            //var_dump($r);
        }
        if ($r['result'] == 1) {
            echo "error 1";
            //$this->session->set_userdata();
            redirect(site_url() . '/admin');
            exit();
        } else if ($r['result'] == 2) {
            echo "error 2";
            redirect(site_url() . '/admin?error=2');
            exit();
        } else if ($r['result'] == 3) {
            echo "error 3";
            redirect(site_url() . '/admin?error=3');
            exit();
        }
        //var_dump($r);
        redirect(site_url() . '/admin?error=unknown');
    }

}
